<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@page import="crm.util.ParamUtil"%>
<%@page import="crm.model.Recepit"%>
<%@page import="crm.control.RecepitControl"%>
<%@page import="crm.control.CustomerControl"%>
<%@page import="crm.control.LinkmanControl"%>
<%@page import="crm.util.GetBaseParam"%>
<%@page import="crm.control.EmployeesControl"%>
<%@page import="crm.model.Employees"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../resources/css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../resources/css/inner.css" type="text/css" media="screen" />
<title>Insert title here</title>
</head>
<script type="text/javascript">
window.onload = function(){
	var btn= document.getElementById("stage");
	var tip = document.getElementById('tip');
	if(tip !=null)
	tip.style.display = 'none';
	btn.onchange=function(){
		if(this.value == 5){
			tip.style.display = 'block';
		} else{
	tip.style.display = 'none';
			}
}
}
</script>
<body>
<div class='init'>
<%
               request.setCharacterEncoding("utf-8");
               int recepitId = ParamUtil.getInt(request,"id",0);
               String update = ParamUtil.getString(request,"action","");
               Recepit re = RecepitControl.getRecepitById(recepitId);
               Employees cu = (Employees)session.getAttribute("user");
               if(update==null||update.equals("")){
%>
<form action="recepit_update.jsp?id=<%=recepitId %>&&action=update" method="post">
<table>
       <tr><td>项目名称</td><td><%=re.getTitle() %></td></tr>
        <tr><td>客户</td><td><%=CustomerControl.getCustomerById(re.getConsumer_id()).getName() %></td></tr>
       <tr><td>联系人</td><td><%=LinkmanControl.getLinkmanName(re.getLinkman_id()) %></td></tr>
       <tr><td>创建人</td><td><%=re.getCreator() %></td></tr>
       <tr><td>意向</td><td><select name="intention">
        <%  
           HashMap intentions = GetBaseParam.getAllIntentions();
          Iterator it = intentions.entrySet().iterator();
          while(it.hasNext()){
    	       Map.Entry entry = (Map.Entry)it.next();
    	  %>
    	  <option value="<%=entry.getKey() %>" <%if(re.getIntention()!=null&&re.getIntention().equals(entry.getKey()+"")) out.print("selected='selected'"); %>><%=entry.getValue() %></option>
    	  <%
      }%></select></td></tr>
       <tr><td>跟单阶段</td><td><%if(re.getStage()==3&&cu.getPosition()==1) out.print("报价审核状态，您无法修改状态。"); else{ %><select name="stage" id="stage" >
  <%  
             HashMap stages = GetBaseParam.getAllRecepitStages();
             Iterator it2 = stages.entrySet().iterator();
             while(it2.hasNext()){
    	          Map.Entry entry = (Map.Entry)it2.next();
    	          if((Integer)entry.getKey()>=re.getStage()&&(Integer)entry.getKey()<=3){  //用于判断当前状态，不能显示此状态以前的。
    	  %>
    	  <option value="<%=entry.getKey() %>" <%if(re.getStage()==Integer.parseInt(entry.getKey()+"")) out.print("selected='selected'"); %>><%=entry.getValue() %></option>
    	  <%
      }}
       if(re.getStage()>=4){
    	   %>
     	  <option value="<%=re.getStage() %>" selected ><%=GetBaseParam.getStageById(re.getStage()) %></option>
     	  <%
       }      
    	         
  %></select><% } %></td></tr>
    	<tr><td>您跟单阶段回款金额</td><td><input type="text" name="huikuan" readonly value="<%=RecepitControl.getMyHuiKuan(recepitId) %>"/>元</td></tr>
       <tr><td>分类</td><td><input type="radio" value="0" name="sort" <%if(re.getSort()!=null&&re.getSort().equals("0")) out.print("checked"); %>>一般
                              <input type="radio" value="1" name="sort" <%if(re.getSort()!=null&&re.getSort().equals("1")) out.print("checked"); %>>重要</td></tr>
       <tr><td>当前跟单人</td><td><%=EmployeesControl.getEmpolyeesById(re.getCurrent_person()).getName() %></td></tr>
       <tr><td>预计成交时间</td><td><%=re.getPredict_time() %></td></tr>
     <%
             float predict_amount = Float.parseFloat(re.getPredict_amount());
             if(predict_amount>=10000){
    	             predict_amount = predict_amount / 10000;
             }
     %>
     
       <tr><td>预计成交金额</td><td><input type="text" value="<%=predict_amount%>" name="predict_amount" /><select name="danwei">
                                         <option value="1" <%if(Integer.parseInt(re.getPredict_amount())>=10000) out.print("selected"); %>>万元</option>
                                         <option value="0" <%if(Integer.parseInt(re.getPredict_amount())<10000) out.print("selected"); %>>元</option>
                                         </select></td></tr>
       <%
            String p[] = re.getPrduct().split("@");
       %>
       <tr><td>天成软件</td><td><%=p[1] %></td> </tr>
       <tr><td>第三方软件</td><td><%=p[2] %></td> </tr>
       <tr><td>相关硬件</td><td><%=p[3] %></td> </tr>
       <tr><td colspan='2'><input class='button' type="submit" value="确认提交"></td></tr>
</table>
</form>
<%}else{
	         re.setLinkman_id(ParamUtil.getInt(request,"linkman_id",re.getLinkman_id()));
	         re.setIntention(ParamUtil.getString(request,"intention",re.getIntention()));
	         re.setStage(ParamUtil.getInt(request,"stage",re.getStage()));
	         re.setSort(ParamUtil.getString(request,"sort",re.getSort()));
	         float  predict_amount = Float.parseFloat(ParamUtil.getString(request,"predict_amount","0"));
	         if(ParamUtil.getInt(request,"danwei",0)==1){
	       	  predict_amount = predict_amount*10000;
	         }
	         re.setPredict_amount(predict_amount+"");
	         
	         //下面的作用是：当跟单进行修改时（状态，回款），将同步更新记录当前跟单人字段的内容，注意此段代码需放在setStage()之后
	         String person = RecepitControl.getCurrentPerson(recepitId);
	         String[] old = person.split("@");
     	     String  newPerson = "@"+old[1]+"@"+old[2]+"@"+re.getStage()+"@";
	         if(re.getStage()==5){
	        	       String hk = ParamUtil.getString(request,"huikuan","0");
	        	       float fhk = Float.parseFloat(hk);
	        	       String danwei = ParamUtil.getString(request,"danwei_huikkuan","0");
	        	       if(danwei.equals("1")){
	        	    	   fhk = fhk*10000;}
	        	       newPerson = newPerson+String.valueOf(fhk)+"@";
	        	 }else{
	        		 newPerson+="0@";
	        	 }
	         int flag=RecepitControl.getTransferRecordNumber(recepitId);
	         if(flag==2)
	        	 re.setPerson1(newPerson);
	         if(flag==3)
	        	 re.setPerson2(newPerson);
	         if(flag==4)
	        	 re.setPerson3(newPerson);
	         if(RecepitControl.updateRecepit(re)){
	        	 %>
	        	 <script>
	        	 alert("修改成功");
	        	 window.top.location.href="show_all_recepits.jsp";
	        	 </script>
	        	 <%
	         }else{
	        	 %>
	        	 <script>
	        	 alert("修改失败");
	        	 history.go(-1);
	        	 </script>
	        	 <%
	         }
} %>
</div>
</body>
</html>